package com.xiaotu.service.sw.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xiaotu.dao.sw.SwCommentInfoMapper;
import com.xiaotu.dao.sw.SwCommentMapper;
import com.xiaotu.dao.tip.MsgTipMapper;
import com.xiaotu.dto.SwCommentDto;
import com.xiaotu.model.MsgTipModel;
import com.xiaotu.model.SwCommentInfoModel;
import com.xiaotu.model.SwCommentModel;
import com.xiaotu.service.sw.SwCommentService;
import com.xiaotu.vo.SwCommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zhangxiao
 * @create 2021-08-30 16:35
 */
@Service
@Transactional
public class SwCommentServiceImpl implements SwCommentService {

    @Autowired
    private SwCommentMapper swCommentMapper;

    @Autowired
    private SwCommentInfoMapper swCommentInfoMapper;

    @Autowired
    private MsgTipMapper msgTipMapper;

    @Override
    public void insert(SwCommentDto swCommentDto) {
        SwCommentModel commentModel = new SwCommentModel();
        SwCommentInfoModel commentInfoModel = new SwCommentInfoModel();
        commentInfoModel.setCommentTime(new Date());
        BeanUtil.copyProperties(swCommentDto, commentInfoModel);
        swCommentInfoMapper.insert(commentInfoModel);
        swCommentDto.setCommentId(commentInfoModel.getCommentId());
        BeanUtil.copyProperties(swCommentDto, commentModel);
        swCommentMapper.insert(commentModel);
        msgTipMapper.updateByPrimaryKey(new MsgTipModel(1, 0));
    }

    @Override
    public List<SwCommentVo> comments(Integer starId) {
        return swCommentMapper.comments(starId);
    }
}
